// featurePackage/pages/giftMain/giftMain.js

import {
    queryGiftDetail,
    buyCard,
    giftList
} from "../../../service/jb_gift_service"
import {
    queryUserInfo,
} from "../../../service/jb_mine_service"
import debounce from "../../../tools/debounce";
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        giftBgImg: "https://images.jiabao19.com/common/lllllll.png",
        menuTop: app.globalData.menuButton.top,
        menuHeight: app.globalData.menuButton.height,
        cardList: [
            // {
            //     id: 0,
            //     money: 50,
            //     isChoose: true,
            // },
            // {
            //     id: 1,
            //     money: 100,
            //     isChoose: false,
            // },
            // {
            //     id: 2,
            //     money: 200,
            //     isChoose: false,
            // },
            // {
            //     id: 3,
            //     money: 500,
            //     isChoose: false,
            // }
        ],
        currentGiftMoney:0,
        refreshFlag:false,
        cardId: null,//选择的卡片id
        userInfo: {
            giftMoney: 0.00
        },
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.queryGiftList();

    },


    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.queryUserInfo();
    },

    //获取个人信息api
    queryUserInfo() {
        queryUserInfo().then(res => {
            if (res.code == 200) {
                this.setData({
                    userInfo: res.data,
                })

            }
        }).catch(err => {
            console.error("JJLog:queryUserInfo", err);
        })
    },

    queryGiftDetail() {
        wx.showLoading();
        queryGiftDetail().then(res => {
            wx.hideLoading();
        }).catch(err => {
            wx.hideLoading();
            wx.showToast({
                title: err || '加载失败',
                icon: "none"
            })
            console.error('JJLog:queryGiftDetail', err);
        })
    },

    pushToGiftDetail() {
        wx.navigateTo({
            url: '/featurePackage/pages/giftRecord/giftRecord',
        })
    },
    pushToBind() {
        wx.navigateTo({
            url: '/featurePackage/pages/giftCardBind/giftCardBind',
        })
    },
    //确定购买卡片
    sureBuyCard() {
        if (!this.data.cardId) {
            wx.showToast({
                title: '请选择要购买的礼品卡',
                icon: "none"
            })
            return;
        }
        debounce(this.queryBuyCard)
    },
    //选择卡片
    selectCardAction(e) {
        let index = e.currentTarget.dataset.index;
        let arr = this.data.cardList;
        arr.forEach(i => {
            i.isChoose = false;
        })
        arr[index].isChoose = true;
        this.setData({
            cardList: arr,
            cardId: arr[index].id,
            currentGiftMoney:arr[index].price
        })
    },
    //购买接口
    queryBuyCard() {
        wx.showLoading();
        buyCard({
            cardId: this.data.cardId,
            payType: "weixin",
            from: "routine"
        }).then(res => {

            if (res.code == 200) {
                let payData = res.data.jsConfig;
                wx.requestPayment({
                    timeStamp: payData.timeStamp,
                    nonceStr: payData.nonceStr,
                    package: payData.packages,
                    signType: 'MD5',
                    paySign: payData.paySign,
                    success: (res) => {
                        wx.hideLoading();
                        console.log("JJLog:微信支付成功回调:" + JSON.stringify(res));
                        wx.showToast({
                            title: '购卡成功',
                            icon: "success",
                            duration: 2000
                        })


                    },
                    fail: (err) => {
                        wx.hideLoading();
                        wx.showToast({
                            title: '支付失败',
                            icon: "none"
                        })
                        console.log("JJLog:微信支付失败回调:" + JSON.stringify(err));
                    }
                });
            }
        }).catch(err => {
            wx.hideLoading();
            wx.showToast({
                title: err || '购买失败',
                icon: "none"
            })
            console.error('JJLog:buyCard', err);
        })
    },

    realPayAction() {

    },






    //查询礼品卡列表
    queryGiftList() {
        wx.showLoading();
        giftList().then(res => {
            wx.hideLoading();
            if (res.code == 200) {
                let arr = res.data || [];
                if (arr.length > 0) {
                    arr.forEach(i => {
                        i.isChoose = false;
                    })
                    this.setData({
                      currentGiftMoney:arr[0].price,
                      cardId:arr[0].id
                    })
                }
                this.setData({
                    cardList: arr,
                    refreshFlag:false,
                })
            }
        }).catch(err => {
            wx.hideLoading();
            this.setData({
             refreshFlag:false,
            })
            wx.showToast({
                title: err || '加载失败',
                icon: "none"
            })
            console.error('JJLog:giftList', err);
        })
    },
    handleRefresh(){
      this.setData({
        refreshFlag:true,
      })
      this.queryUserInfo();
      this.queryGiftList();
    },
    pushToRule(){
      wx.navigateTo({
        url: '/featurePackage/pages/giftRule/giftRule',
      })
    },
    pushToRecord(){
      wx.navigateTo({
        url: '/featurePackage/pages/giftRecord/giftRecord?listType=2',
      })
    },

    // /**
    //  * 用户点击右上角分享
    //  */
    // onShareAppMessage() {

    // }
})